from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

def plot_color_distribution(image_path):
    # 读取图片并转换为RGB格式
    img = Image.open(image_path).convert('RGB')
    img_array = np.array(img)

    # 提取RGB三个通道
    r = img_array[:, :, 0].flatten()
    g = img_array[:, :, 1].flatten()
    b = img_array[:, :, 2].flatten()

    # 设置直方图参数
    bins = 256
    range = (0, 256)

    # 绘制三通道直方图
    plt.figure(figsize=(10, 5))
    plt.hist(r, bins=bins, range=range, color='red', alpha=0.5, label='Red')
    plt.hist(g, bins=bins, range=range, color='green', alpha=0.5, label='Green')
    plt.hist(b, bins=bins, range=range, color='blue', alpha=0.5, label='Blue')

    # 添加图例和标题
    plt.legend(loc='upper right')
    plt.title('RGB Color Distribution')
    plt.xlabel('Color Value')
    plt.ylabel('Pixel Count')

    # 显示图表
    plt.show()

if __name__ == "__main__":
    image_path = "./cleaned.png"
    plot_color_distribution(image_path)
